<?php
class Network extends CI_Model
{
	public $tbl_name = 'networks';

	public $col_networkId = 'networkId';
	public $col_networkName = 'networkName';
	public $col_pubIdOrAppId = 'pubIdOrAppId';
	public $col_passOrSecretKey = 'passOrSecretKey';
	public $col_lockStatus = 'lockStatus';
	public $col_networkType = 'networkType';
	public $col_ratio = 'ratio';
	

	public function __contruct()
	{
		parent::__construct();	
	}


	public function getNetworks()
	{		
		$query = $this->db->get($this->tbl_name);
		return $query->result();
	}


	public function getNetworkById($networkId)
	{
		$this->db->where($this->col_networkId, $networkId);
		$this->db->limit(1);

		$query = $this->db->get($this->tbl_name);

		return $query->row();
	}


	public function getNetworksByType($networkType)
	{
		$this->db->where($this->col_networkType, $networkType);
		$this->db->where($this->col_lockStatus, 0);
		
		$query = $this->db->get($this->tbl_name);
		return $query->result();
	}

	public function getWall($networkId)
	{
		$this->db->where($this->col_networkType, WALL);
		$this->db->where($this->col_lockStatus, 0);
		$this->db->where($this->col_networkId, $networkId);
		$this->db->limit(1);

		$query = $this->db->get($this->tbl_name);

		return $query->row();
	}


	public function getBanner($networkId)
	{
		$this->db->where($this->col_networkType, BANNER);
		$this->db->where($this->col_lockStatus, 0);
		$this->db->where($this->col_networkId, $networkId);
		$this->db->limit(1);

		$query = $this->db->get($this->tbl_name);

		return $query->row();
	}

	public function getBanner_ratio($networkId)
	{
		$this->db->where($this->col_networkId, $networkId);
		$this->db->limit(1);

		$query = $this->db->get($this->tbl_name);

		return $query->row()->ratio;
	}	

	public function getPassOrSecretKey($networkId)
	{
		$this->db->where($this->col_networkId, $networkId);
		$this->db->limit(1);

		$query = $this->db->get($this->tbl_name);

		return $query->row()->passOrSecretKey;
	}


	public function update($networkId, $networkName, $pubIdOrAppId, $passOrSecretKey, $ratio, $lockStatus)
	{
		$lock = ($lockStatus == 0) ? FALSE : TRUE;
		$data = array(
		               $this->col_networkName => $networkName,
		               $this->col_pubIdOrAppId => $pubIdOrAppId,
		               $this->col_passOrSecretKey => $passOrSecretKey,
		               $this->col_ratio => $ratio,
		               $this->col_lockStatus => $lock
		            );

		$this->db->where($this->col_networkId, $networkId);
		$this->db->update($this->tbl_name, $data); 		
	}

}
?>